package com.glut.campusrepair.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.glut.campusrepair.service.IMaterialsManageService;
import com.glut.campusrepair.entity.MaterialsManage;
import com.glut.campusrepair.mapper.MaterialsManageMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 材料管理表 服务实现类
 *
 * @author sanminzhi
 * @since 2024-07-19 11:26
 */
@Service
@Slf4j
public class MaterialsManageServiceImpl extends ServiceImpl<MaterialsManageMapper, MaterialsManage> implements IMaterialsManageService {

    @Autowired
    MaterialsManageMapper materialsManageMapper;

    @Override
    public List<MaterialsManage> selectAll() {
        return list();
    }

    @Override
    public Page<MaterialsManage> selectPage(int page, int size, String category) {
        QueryWrapper<MaterialsManage> queryWrapper = new QueryWrapper<>();
        if (category != null && !category.isEmpty()) {
            queryWrapper.eq("category", category);
        }
        return page(new Page<>(page, size), queryWrapper);
    }

    @Override
    public List<MaterialsManage> getMaterialsManage(){
        LambdaQueryWrapper<MaterialsManage> materialsManage = new LambdaQueryWrapper<>();
        return this.list(materialsManage);
    }

    @Override
    public List<MaterialsManage> findMaterials(String materialName) {

        LambdaQueryWrapper<MaterialsManage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MaterialsManage::getMaterialName,materialName);

        // 执行查询，获取符合条件的所有结果
        List<MaterialsManage> materialsManages = list(queryWrapper);
        log.info("查询{}",materialName);
        log.info("查询{}",materialsManages);
        return materialsManages;
    }

    @Override
    public List<MaterialsManage> getLowStockMaterials(int threshold) {
        LambdaQueryWrapper<MaterialsManage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.lt(MaterialsManage::getQuantity, threshold);
        return list(queryWrapper);
    }


}
